package srzy.driver

import srzy.common.Logging
import srzy.dao.DBUtil
import srzy.common.Common
import spray.json._
import DefaultJsonProtocol._

object Startb extends Logging {

  def main(args: Array[String]): Unit = {
    //    deal1
    //    deal2
    //    deal3
    deal10

    //    deal2
    //    val cdrRootPaths = "/home/hn_sm/ocssm/cdr/0/cur"
    //    val cdrPaths = srzy.module.ReadCdrFile.getDirectory(new java.io.File(cdrRootPaths), "")
    //    println(cdrPaths.map(x => x._1.getPath() + " " + x._2).mkString("\n"))
  }

  def deal10 = {
    val l = DBUtil("MikuSource")
    val ll = l.getCaseInfo.toList.map(x =>
      (x._1, x._2.stdDCCReceMsg, x._2.stdRBReceFromSM, x._2.stdRBSendToSM))
    val l2 = DBUtil("CaseInfoSource")
    val lll = Common.readFromFile2("cdr2.txt").asJson.convertTo[List[(String, String)]].toMap

    ll.foreach(x => {
      logger.debug("case:" + x._1)
      lll.get(x._1) match {
        case Some(v) => l2.updateCaseInfo(x._1, v, x._2, x._3, x._4)
        case None => logger.debug("not found")
      }
    })

    //    Common.writeToFile("cdr0.txt", ll.toJson.prettyPrint)
  }

  def deal0 = {
    val l = DBUtil("CaseInfoSource")
    val ll = l.getCaseInfo.toList.map(x =>
      (x._1, x._2.stdCdr))
    Common.writeToFile("cdr0.txt", ll.toJson.prettyPrint)
  }

  def undeal0 = {
    val l = DBUtil("CaseInfoSource")
    val ll = Common.readFromFile2("cdr0.txt").asJson.convertTo[List[(String, String)]]
    ll.foreach(e => {
      logger.debug("case:" + e._1)
      l.updateCdrInfo(e._1, e._2)
    })
  }

  def deal1 = {
    val l = DBUtil("CaseInfoSource")
    val ll = l.getCaseInfo0.toList.map(x =>
      (x._1, x._2._2.split("\n").toList.filter(_ != "").map((x._2._1, _))))
    Common.writeToFile("cdr1.txt", ll.toJson.prettyPrint)
  }

  def deal2 = {
    val ll = Common.readFromFile2("cdr1.txt").asJson.convertTo[List[(String, List[(String, String)])]]
    val lll = ll.map(x => {
      val ee: List[(String, String)] = x._2.map(e =>
        (e._1 match {
          case "data" => "1.Data.NORMAL_FILE_OUTPUT"
          case "sms" => "1.Sms.NORMAL_FILE_OUTPUT"
          case "vac" => "1.Vas.NORMAL_FILE_OUTPUT"
          case "voice" => "1.Voice.NORMAL_FILE_OUTPUT"
          case "wlan" => "1.Dsl.NORMAL_FILE_OUTPUT"
          case "offData" => "1.Data.OFFLINE_FILE_OUTPUT"
          case "offVoice" => "1.Voice.OFFLINE_FILE_OUTPUT"
          case "offWlan" => "1.Dsl.OFFLINE_FILE_OUTPUT"
        }, e._2))
      val text = ee.toJson.prettyPrint
      (x._1, text)
    })
    Common.writeToFile("cdr2.txt", lll.toJson.prettyPrint)
    //    "/home/hn_sm/ocssm/cdr/0/cur"
  }

  def deal3 = {
    val l = DBUtil("CaseInfoSource")

    val lll = Common.readFromFile2("cdr2.txt").asJson.convertTo[List[(String, String)]]
    lll.foreach(e => {
      logger.debug("case:" + e._1)
      l.updateCdrInfo(e._1, e._2)
    })
  }

}

